package cn.zjgsu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.zjgsu.domain.TbCourse;
import cn.zjgsu.domain.TbStudent;
import cn.zjgsu.domain.TbTask;
import cn.zjgsu.domain.TbTeacher;
import cn.zjgsu.domain.TbUser;
import cn.zjgsu.mapping.TbCourseMapper;
import cn.zjgsu.mapping.TbStudentMapper;
import cn.zjgsu.mapping.TbTaskMapper;
import cn.zjgsu.mapping.TbTeacherMapper;
import cn.zjgsu.mapping.TbUserMapper;
import cn.zjgsu.service.IStudentService;
import cn.zjgsu.utils.SHA1;


@Service
public class StudentServiceImpl implements IStudentService{

	@Autowired
	private TbStudentMapper studentMapper;
	@Autowired
	private TbTeacherMapper teacherMapper;
	@Autowired
	private TbUserMapper userMapper;
	@Autowired
	private TbTaskMapper taskMapper;
	@Autowired
	private TbCourseMapper courseMapper;
	@Override
	public TbStudent getStudentByFkUser(int fkUser) {
		TbStudent student=studentMapper.selectByForeignKey((long)fkUser);
		return student;
	}

	@Override
	public void updateStudent(TbStudent student) {
		TbStudent Ostudent = studentMapper.selectByStudentNum(student.getStudentNum());
		student.setPkStudent((Ostudent.getPkStudent()));
		studentMapper.updateByPrimaryKeySelective(student);
		
	}

	@Override
	public void changeStudentPassword(int pkStudent, String changePassword) {
	
	TbStudent student =studentMapper .selectByPrimaryKey((long)pkStudent);	
	int pkUser=student.getFkUser();
	TbUser user= userMapper.selectByPrimaryKey((long)pkUser);
		if(student.getFkUser()!=null)
		{
			student .setStudentPwd(SHA1.encrypt(changePassword));
			user.setPassword(SHA1.encrypt(changePassword));
			 userMapper.updateByPrimaryKeySelective(user);
		 studentMapper.updateByPrimaryKeySelective(student);
		}
		
		
	}

	@Override
	public TbStudent selectByStudentNum(String studentNum) {
		TbStudent student=studentMapper.selectByStudentNum(studentNum);
		return student;
	}

	@Override
	public int getTaskCount(int fkUser) {
		int count=(int)taskMapper.getCount(fkUser);
		return count;
	}
	

	@Override
	public List<TbTask> selectByPage(int startPos, int pageSize, int fkUser) {
		List<TbTask> list=taskMapper.selectByPage(startPos, pageSize,fkUser);
		return list;
	}

	@Override
	public List<String> getYear(Integer pkStudent) {
		
		List<String> list=courseMapper.getStudentYear(pkStudent);
		//这一部分是数据库获取的年份ArrayList去重
			 for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )   {   
				    for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )   {   
				      if  (list.get(j).equals(list.get(i)))   {   
				        list.remove(j);   
				      }   
				    }   
				  }   

		return list;
	}

	@Override
	public List<TbCourse> findCourseRecordByYear(Integer pkStudent, String year) {

		List<TbCourse> list=courseMapper.findCourseRecordByStudentYear(pkStudent, year);
	
		 for  ( int  i  =   0 ; i  <  list.size() ; i ++ )   {   
			   int  pkTeacher=list.get(i).getFkTeacher();
			   TbTeacher teacher=teacherMapper.selectByPrimaryKey(pkTeacher);
			   
			   list.get(i).setTeacherName(teacher.getTeacherName());
				System.out.println(list.get(i).getTeacherName());
			  }  
		
		return list;
	}

}
